package com.galaxy.samson.student.dao;

import com.galaxy.samson.student.domain.Student;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: arunachalam
 * Date: 2/6/12
 * Time: 7:42 PM
 * To change this template use File | Settings | File Templates.
 */
public class StudentDao implements IStudentDao
{
    @PersistenceContext
    private EntityManager entityManager;
    @Override
    public void insert(Student student)
    {
             entityManager.persist(student);
    }

    @Override
    public void removeAll()
    {
        Query query = entityManager.createNamedQuery("removeAll");
        query.executeUpdate();
    }

    @Override
    public void remove(String registerNumber)
    {
        Query query = entityManager.createQuery("delete from Student s where s.registerNumber=:registerNumber");
        query.setParameter("registerNumber",registerNumber);
        query.executeUpdate();
//        entityManager.remove(student);
    }

    @Override
    public List<Student> findAll()
    {
        Query query = entityManager.createQuery("select s from Student s");
        return query.getResultList();
    }

    @Override
    public Student searchByRegisterNumber(String registerNumber)
    {
        Query query = entityManager.createNamedQuery("searchByRegisterNumber");
        query.setParameter("registerNumber",registerNumber);
        return (Student)query.getSingleResult();
    }

    @Override
    public Student searchByRollNumber(int rollNumber)
    {

        Query query = entityManager.createNamedQuery("searchByRollNumber");
        query.setParameter("rollNumber",rollNumber);
       return (Student)query.getSingleResult();

    }

    @Override
    public List<Student> searchByDepartment(String department)
    {
        Query query = entityManager.createNamedQuery("searchByDepartment");
        query.setParameter("department",department);
        return query.getResultList();
    }

    @Override
    public List<Student> searchByCity(String city)
    {
        Query query = entityManager.createNamedQuery("searchByCity");
        query.setParameter("city",city);
        return query.getResultList();

    }

    @Override
    public List<Student> searchByName(String name)
    {
        Query query = entityManager.createNamedQuery("searchByName");
        query.setParameter("name",name);
        return query.getResultList();
    }
}
